package com.ikas.ai.server.module.alarm.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.model.PageQuery;
import com.ikas.ai.server.aop.SpecialPermission;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.alarm.service.UnitDeteriorationAlarmService;
import com.ikas.ai.server.module.instance.model.DorisWorkInstanceStepDetail;
import com.ikas.ai.utils.PageExUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 机组劣化预警
 *
 * @author tang.xuandong
 * @version 1.0.0
 * @date 2023/2/1 10:49
 */
@RestController
@RequestMapping("/alarm/unit")
public class UnitDeteriorationAlarmController extends AbstractBean {

    @Autowired
    private UnitDeteriorationAlarmService unitDeteriorationAlarmService;

    /**
     * 机组劣化预警分页列表
     *
     * @param machineNo
     * @param wcDesc
     * @param solved
     * @param startWcBatch
     * @param endWcBatch
     * @return
     */
//    @Pageable(sortType = "desc", sortBy = "wc_batch")
    @GetMapping("/page")
    public JsonResult queryAlarm(
            @RequestParam(required = false, value = "machineNo") String machineNo,
            @RequestParam(required = false, value = "wcDesc") String wcDesc,
            @RequestParam(required = false, value = "solved") Integer solved,
            @RequestParam(required = false, value = "startWcBatch") String startWcBatch,
            @RequestParam(required = false, value = "endWcBatch") String endWcBatch,
            @RequestParam(required = false, value = "stepName") String stepName,
            @RequestParam(required = false, value = "meteCode") String meteCode
            , PageQuery pageQuery) {
        PageExUtil.configPageHelper(pageQuery);
        return JsonResult.ok(unitDeteriorationAlarmService.queryAlarm(machineNo, wcDesc, startWcBatch, endWcBatch, solved, stepName, meteCode));
    }

    /**
     * 获取某个具体工况流程劣化预警记录列表
     *
     * @param wcDesc
     * @param wcBatch
     * @return
     */
    @GetMapping("/listByWc")
    public JsonResult listByWc(
            @RequestParam(value = "machineNo") String machineNo,
            @RequestParam(value = "wcDesc") String wcDesc,
            @RequestParam(value = "wcBatch") String wcBatch) {
        return JsonResult.ok(unitDeteriorationAlarmService.listByWc(machineNo, wcDesc, wcBatch));
    }

    /**
     * 劣化预警记录新增
     *
     * @param workInstanceStepDetail
     * @return
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody DorisWorkInstanceStepDetail workInstanceStepDetail) {
        unitDeteriorationAlarmService.add(workInstanceStepDetail);
        return JsonResult.ok();
    }

    /**
     * 消缺
     *
     * @param id
     * @return
     */
    @GetMapping("/solved/{id}")
    @SpecialPermission(value = {"alarm:solved"})
    public JsonResult solved(@PathVariable("id") Long id) {
        unitDeteriorationAlarmService.solved(id, getUser());
        return JsonResult.ok();
    }

    /**
     * 劣化记录消缺后重新开启
     *
     * @param id
     * @return
     */
    @GetMapping("/reOpen")
    public JsonResult reOpenForTest(Long id) {
        unitDeteriorationAlarmService.reOpenForTest(id);
        return JsonResult.ok();
    }
}
